package com.neusoft.license.api.init;

import com.neusoft.license.api.license.LicenseVerify;
import com.neusoft.license.api.param.LicenseVerifyParam;
import com.neusoft.license.common.constant.RestConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Author: xlh
 * @Description:
 * @Date: Create in 17:09 2020/9/17 0017
 */
@Slf4j
@Component
public class LicenseListener implements ApplicationListener<ContextRefreshedEvent> {
    /**
     * 证书subject
     */
    private String subject = "东软集团股份有限公司";

    /**
     * 公钥别称
     */
    private String publicAlias = "publicCert";

    /**
     * 访问公钥库的密码
     */
    private String storePass = "public_password1234";

    private final static AtomicBoolean atomicBol = new AtomicBoolean(false);

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (!atomicBol.get()) {
            log.info(RestConstant.LICENSE_INSTALLING);
            LicenseVerifyParam param = new LicenseVerifyParam();
            param.setSubject(subject);
            param.setPublicAlias(publicAlias);
            param.setStorePass(storePass);
            LicenseVerify licenseVerify = new LicenseVerify();
            //安装License
            licenseVerify.install(param);
            log.info(RestConstant.LICENSE_FINISH);
            atomicBol.set(true);
        }
    }
}
